package org.vidyakiran.app.client.service.suggestion;

import org.vidyakiran.app.client.service.SuggestionService;
import org.vidyakiran.app.client.service.SuggestionServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.SuggestOracle;

public class ItemSuggestOracle extends SuggestOracle {
	
	private String suggestionType="";
	public static String DELIMITER = ":";
	/**
	 * @return the suggestionType
	 */
	public String getSuggestionType() {
		return suggestionType;
	}

	/**
	 * @param suggestionType the suggestionType to set
	 */
	public void setSuggestionType(String suggestionType) {
		this.suggestionType = suggestionType;
	}

	@Override
	public void requestSuggestions(SuggestOracle.Request request,
			SuggestOracle.Callback callback) {
		request.setQuery(suggestionType+DELIMITER+request.getQuery());
		SuggestionServiceAsync suggestionServiceAsync=(SuggestionServiceAsync) GWT.create(SuggestionService.class);   
		suggestionServiceAsync.getSuggestions(request, new ItemSuggestCallback(request, callback));
	}
	
	public boolean isDisplayStringHTML() {
		return true;
	}

	

	class ItemSuggestCallback implements AsyncCallback {
		private SuggestOracle.Request req;
		private SuggestOracle.Callback callback;

		public ItemSuggestCallback(SuggestOracle.Request _req,
				SuggestOracle.Callback _callback) {
			req = _req;
			callback = _callback;
		}

		public void onFailure(Throwable error) {
			callback.onSuggestionsReady(req, new SuggestOracle.Response());
		}

		public void onSuccess(Object retValue) {
			callback.onSuggestionsReady(req, (SuggestOracle.Response) retValue);
		}
	}
}
